# -*- coding: utf-8 -*-
"""
Created on Fri Nov 30 16:57:40 2018

@author: Administrator
"""

import time, datetime
from pytsdb import xtsdb
import pyxtsdb
import pandas as pd
xdb = xtsdb(host='10.70.248.146')
tk = xdb.getToken()
print(tk)
# 抽取数据, 以1号x光源高压	Status.XRay1HighVoltage 和为例
# 1号x光源管电流	Status.XRay1TubeCurrent
# 1号和2号
r = xdb.getHistoryByTagNames(collector_name='BGTATOEMFN',tag_names=["BS_650784E01_MOTOR_BODY_WINDING_TEMP1"],start_time='20221201171100',end_time='20221202171100')


r = xdb.getHistoryByTagNames('tongweisu1580',["Status.XRay1HighVoltage","Status.XRay1TubeCurrent"],minutes=1000)

X1HV = r["Status.XRay1HighVoltage"]["records"]
#print(X1HV)
X1HV_df = pd.DataFrame(X1HV)
#print(X1HV_df)
X1HV_df.to_csv("D:/code/work/XRAY/data/XRay1HighVoltage.csv")

X1TC = r["Status.XRay1TubeCurrent"]["records"]
X1TC_df = pd.DataFrame(X1TC)
#print(X1TC_df)
#print(X1TC_df['quality'])
#
#X1TC_df['time'] = 0
#print(X1TC_df) 
X1TC_df.to_csv("D:/code/work/XRAY/data/XRay1TubeCurrent.csv")

# 2号

r = xdb.getHistoryByTagNames('tongweisu1580',["Status.XRay2HighVoltage","Status.XRay2TubeCurrent"],minutes=1000)
X2HV = r["Status.XRay2HighVoltage"]["records"]
#print(X1HV)
X2HV_df = pd.DataFrame(X2HV)
#print(X1HV_df)
X2HV_df.to_csv("D:/code/work/XRAY/data/XRay2HighVoltage.csv")

X2TC = r["Status.XRay2TubeCurrent"]["records"]
X2TC_df = pd.DataFrame(X2TC)
#print(X1TC_df)
#print(X1TC_df['quality'])
#
#X1TC_df['time'] = 0
#print(X1TC_df) 
X2TC_df.to_csv("D:/code/work/XRAY/data/XRay2TubeCurrent.csv")

# 1号x光源灯丝电流 Status.XRay1FilamentCurrent
r = xdb.getHistoryByTagNames('tongweisu1580',["Status.XRay1FilamentCurrent","Status.XRay1ChopperVoltage"],minutes=1000)
X2HV = r["Status.XRay2HighVoltage"]["records"]
#print(X1HV)
X2HV_df = pd.DataFrame(X2HV)
#print(X1HV_df)
X2HV_df.to_csv("D:/code/work/XRAY/data/XRay1FilamentCurrent.csv")

X2TC = r["Status.XRay2TubeCurrent"]["records"]
X2TC_df = pd.DataFrame(X2TC)
X2TC_df.to_csv("D:/code/work/XRAY/data/XRay1ChopperVoltage.csv")


#timeArray = time.localtime(X1TC_df['timestamp'])
## 日期格式转换
#timeStamp = 1381419600
#timeArray = time.localtime(timeStamp)
#print(timeArray)
#otherStyleTime = time.strftime("%Y/%m/%d:%H:%M:%S", timeArray)
#print(otherStyleTime)   
### 使用datetime
#timeStamp = 1381419600
#dateArray = datetime.datetime.utcfromtimestamp(timeStamp)
#otherStyleTime = dateArray.strftime("%Y--%m--%d %H:%M:%S")
#print(otherStyleTime)
